<?php

class spRedbean
{
	static protected $table = "";
	static protected $list_title = "title";
	static protected $schema = array();
	private $bean;
	private $elements = null;
	
	function __construct( $id=false, $bean=null )
	{
		if ( $id!==false && $id==0 )
		{
			echo "<div class='error'>";
			echo "ERROR: spRedBean id=0 not a valid id for class: ".get_class($this); 
			var_dump( debug_backtrace() );
			echo "</div>";
			die();
		}
	
		if ( $bean )
		{
			self::setBean( $bean );
		}
		else
		{
			if ( $id!==false )
				$this->bean = R::load( $this->getTable(), $id );
			else
				$this->bean = R::dispense( $this->getTable() );
		}
	}
	
	static public function getSchemaData( $sql_col )
	{
		foreach ( static::getSchema() as $s )
		{
			if ( $s["sql_col"]==$sql_col )
				return $s;
		}
		
		return false;
	}

	static public function getSqlColumnsAsList()
	{
		$list = array();
		
		foreach ( static::getSchema() as $s )
		{
			$list[] = array( "value"=>$s["sql_col"], "content"=>$s["sql_col"] );
		}
		
		return $list;
	}
	
	static public function getSqlColumnsAsArray()
	{
		$a = array();
		
		foreach ( static::getSchema() as $s )
		{
			$a[] = $s["sql_col"];
		}
		
		return $a;
	}
	
	static public function wipe()
	{
		R::wipe( static::$table );
	}

	public function setData( $arData )
	{
		foreach ( $arData as $k => $v )
		{
			self::getBean()->{$k} = $v;
		}
	}
	
	/*
	 $ar = array( 
			array("col"=>"mycol", "formatted"=>true ),
			array("col"=>"mycol", "formatted"=>true ),
			array("col"=>"mycol", "formatted"=>true )
		);
	*/
	public function getColumnAsArray( $arInfo )
	{
		$ar = array();
		foreach ( $arInfo as $a )
		{
			$ar[] = $this->getValue( $a["col"], $a["formatted"] );
		}
		
		return $ar;
	}
	
	// returns the values for the fields in the $arFields
	public function getFieldsAsArray( $arColumns, $formatted=false )
	{
		$ar = array();
		foreach ( $arColumns as $col )
		{
			$ar[] = $this->getValue( $col, $formatted );
		}
		
		return $ar;
	}
	
	public function getValue( $column, $formatted=false )
	{
		return self::getBean()->$column;
	}
	
	public function updateFromDatabase()
	{
		foreach ( self::getSchema() as $s )
		{
			// the sql_col is also the post back name
			$sql_col = $s["sql_col"];
			
			$element = $this->getElement( $sql_col );
			if ( $element==false )
			{
				echo "<div class='error'>ERROR: spRedbean.php -> updateFromDatabase getElement failed with sql_col: ".$sql_col;
				echo "<br/><br/>";
				var_dump( debug_backtrace() );
				echo "</div>";
			}
			else
				$element->setElementDataString( self::getBean()->{$sql_col} );
		}
	}
	
	public function updateFromRequest()
	{
		foreach ( self::getSchema() as $s )
		{
			// the sql_col is also the post back name
			$sql_col = $s["sql_col"];
			
			$element = $this->getElement( $sql_col );
			$element->updateFromRequest();
			self::getBean()->{$sql_col}	= $element->getElementDataString();
		}
	}
	
	protected function setBean( $bean )
	{
		$this->bean = $bean;
	}
	
	static public function getColumns()
	{
		return R::getAll( "SHOW COLUMNS FROM ".static::$table );
	}
	
	static public function remove( $id )
	{
		$bean = R::load( static::$table, $id );
		R::trash( $bean );
	}
	
	protected function createElements()
	{
		$this->elements = array();
		
		foreach ( static::getSchema() as $s )
		{
			$this->elements[] = array(
				"sql_col"	=> $s["sql_col"],
				"title" 	=> $s["title"],
				"element"	=> $this->createElementFromSchema( $s )
			);
		}

		return $this->elements;
	}
	
	public function getElements()
	{
		if ( $this->elements==null )
			$this->elements = $this->createElements();
	
		return $this->elements;
	}

	public function getElement( $sql_col )
	{
		$elements = $this->getElements();
		
		foreach ( $elements as $e )
		{
			if ( $e["sql_col"]==$sql_col )
				return $e["element"];
		}
		
		return false;
	}	
	
	// override this for custom elements
	protected function getElementCustom( $sql_col )
	{
		return 0;
	}
	
	public function createElementFromSchema( $schemaData )
	{
		if ( $schemaData!=false )
		{
			$sql_col = $schemaData["sql_col"];
		
			// not completed
			if ( $schemaData["type"]=="spFormComponents" )
			{
				return new spFormComponents( array(
					"name" => $sql_col
				));
			}
		
			if ( $schemaData["type"]=="custom" )
			{
				return $this->getElementCustom( $sql_col );
			}

			if ( $schemaData["type"]=="combobox" )
			{
				$list = call_user_func( $schemaData["list"] );
			
				return new spCombobox( array(
					"name"	=> "{$sql_col}",
					"list" => $list
				));
			}
			
			if ( $schemaData["type"]=="textarea" )
			{
				return new spTextArea( array(
					"name"=>"{$sql_col}"
				)); 
			}
		
			if ( $schemaData["type"]=="date" )
			{
				return new spDate( array(
					"date_name"=>"{$sql_col}"
				));
			}
		
			if ( $schemaData["type"]=="string" )
			{
				return new spInput(array(
					"type" => "text",
					"name" => "{$sql_col}"
				));				
			}

			if ( $schemaData["type"]=="string4" )
			{
				return new spInputText(	array(
					"class" => "input_cpr",
					"name" => "{$sql_col}",
					"attr" => array(
						"maxlength"=>"4"
						)
					));
			}
			
			if ( $schemaData["type"]=="array_of_text" )
			{
				$element = new spAddToListInputText( array(
					"name" => $sql_col
				));

				return $element;
			}

			if ( $schemaData["type"]=="array_of_checkmarks" )
			{
				$list = call_user_func( $schemaData["list"] );
			
				return new spListCheckbox( array(
					"name"	=> $sql_col,
					"list"	=> $list
				));
			}

			if ( $schemaData["type"]=="array_of_comboboxes" )
			{
				$list = call_user_func( $schemaData["list"] );
			
				return new spAddToListCombo( array(
					"name" 	=> $sql_col,
					"list"	=> $list
				));
			}

			if ( $schemaData["type"]=="array_of_combo_text_text" )
			{
				$list = call_user_func( $schemaData["list"] );
				
				$element = new spAddToListComboTextText( array(
					"name" => $sql_col,
					"list" => $list
				));
				
				return $element;
			}
			
			if ( $schemaData["type"]=="array_of_combo_text_bool" )
			{
				$list = call_user_func( $schemaData["list"] );

				return new spAddToListComboAndTextBool( array(
					"name" => $sql_col,
					"list" => $list
				));
			
			}			
		}

		return false;
	}

	public function save()
	{
		$id = R::store( $this->bean );
		return $id;
	}
	
	public function getId()
	{
		return $this->bean->id;
	}
	
	static function getList()
	{
		$bean = R::find( static::$table );

		$list = array();
		foreach ( $bean as $i )
		{
			$list[] = array( "value"=>$i["id"], "content"=>$i[static::$list_title] );
		}

		return $list;	
	}
	
	static function getSchema()
	{
		return static::$schema;
	}
	
	static function getTable()
	{
		return static::$table;	
	}
	
	public function getBean()
	{
		return $this->bean;
	}
	
}

?>